#include <QString>
#include <QtTest>
#include <QSharedPointer>

class PointerTest : public QObject
{
    Q_OBJECT

public:
    PointerTest();

private Q_SLOTS:
    void test_share_pointer();
    void test_a();
};

class TestA
{
public:
    TestA(int a);
    ~TestA();
    void show() { qDebug()<<"a="<<m_a; }
private:
    int m_a;
};

TestA::TestA(int a) : m_a(a)
{
    qDebug("%s[%d]: %d",__func__, __LINE__, m_a);
}

TestA::~TestA()
{
    qDebug("%s[%d]: %d",__func__, __LINE__, m_a);
}

PointerTest::PointerTest()
{
    QSharedPointer<QString> str1;
    QSharedPointer<QString> *str2;

    {
    QSharedPointer<QString> str0(new QString("string 0"));
    str1 = str0;
    str2 = &str0;

    qDebug()<<"str0:"<<str0.data()<<*str0;
    qDebug()<<"str1:"<<str1.data()<<*str1;
    qDebug()<<"str2:"<<str2->data()<<*  *str2;
    }
//    QSharedPointer::dynamicCast<
    qDebug()<<"str1:"<<str1.data()<<*str1;
}

void PointerTest::test_share_pointer()
{
    QSharedPointer<TestA> a(new TestA(5));
    QSharedPointer<TestA> b = a;

    qDebug("%s[%d]: ",__func__, __LINE__);
    a = QSharedPointer<TestA>(new TestA(8));
    qDebug("%s[%d]: ",__func__, __LINE__);
    b = QSharedPointer<TestA>(new TestA(10));
    qDebug("%s[%d]: ",__func__, __LINE__);
}

void PointerTest::test_a()
{
    QSharedPointer<TestA> a1(new TestA(2));
    a1->show();

    QSharedPointer<TestA> a2;
    a2 = a1;
    a2->show();

    a1.reset(new TestA(5));
    a1->show();
    a2->show();
}

QTEST_APPLESS_MAIN(PointerTest)

#include "main.moc"
